lexer grammar Analex;

options {
  language = Java;
  TokenLabelType=MiToken;
}
 tokens  {
 LIT_REAL;
 LIT_ENTERO;
 
 }

@header {
package PL.prac4;
}
 @rulecatch { catch (RecognitionException e) {
  throw e;}
  }
  
@lexer::members {
public class MiToken extends CommonToken {
  private String nuevoAtributo;
  
  public MiToken (CharStream input, int type, int channel, int start, int stop) {
    super(input, type, channel, start, stop);
    nuevoAtributo = getSourceName();
  }
  public void setNuevoAtributo(String s) {
  nuevoAtributo=s;
  }
  public String getNuevoAtributo() {
  return nuevoAtributo;
  }
  public String toString() {
  String aux;
  aux = "Linea " + getLine() + ", "+ getCharPositionInLine()+ ": encontrado "+ getText() + ", Regla: " + getType() + ". Fichero: " + nuevoAtributo;
  return aux;
  }
}
// override standard token emission
public Token emit() {
    MiToken t = new MiToken(input, state.type, state.channel,
                    state.tokenStartCharIndex, getCharIndex()-1);
    t.setLine(state.tokenStartLine);
    t.setText(state.text);
    t.setCharPositionInLine(state.tokenStartCharPositionInLine);
    emit(t);
    return t;
}

}
 
 //Tipos
 INT: 'i'|'I' 'n'|'N' 't'|'T'; 
 VOID: 'v'|'V' 'o'|'O' 'i'|'I' 'd'|'D';
 //instrucciones
 IF: 'i'| 'I' 'f'|'F';
 WHILE: 'w'|'W' 'i'|'I' 'l'| 'L' 'e'|'E';

fragment NUEVA_LINEA : ('\r\n'|'\n');
fragment ESPACIO: (' ');
BLANCO: (ESPACIO|'\t'|'\r'|NUEVA_LINEA) {$channel = HIDDEN;}; 

fragment DIGITO : '0'..'9'; 
NUMERO : ((DIGITO)+ '.') => (DIGITO)+ '.' (DIGITO)*{ $type = LIT_REAL; }| (DIGITO)+{ $type = LIT_ENTERO; };

fragment LETRA: ('a'..'z')|('A'..'Z');
  IDENT: LETRA (DIGITO|LETRA)*;
  
  OPERADOR: '+' | '-' | '/' | '*' | '++' | '--';
  PARENTESIS: '(' | ')';
  LLAVES: '{' | '}';
  COMA: ',';
  AMPERSAND: '&';
  COMPARADOR: '<' | '>' | '<=' | '>=';
  SEPARADOR: ';';
  IGUAL: '=';
  CADENA: '"' (LETRA|DIGITO|'\\'|'%'|BLANCO)* '"';

COMENTARIO: '/*' ('*' ~('/')| ~('*'))* '*'? '*/' {$channel = HIDDEN;};
//COMENTARIO: '/*' (options {greedy=false;}:.)* '*/'{$channel = HIDDEN;};
LINEA_COMENTARIO: '//' (.)* NUEVA_LINEA {$channel = HIDDEN;};